package com.aiatomic.couting.account;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.aiatomic.couting.AccountInfoActivity;
import com.aiatomic.couting.ICodeMsg;
import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.LoginActivity;
import com.aiatomic.couting.PayActivity;
import com.aiatomic.couting.R;
import com.aiatomic.couting.models.GoodEntity;
import com.aiatomic.couting.util.AppUtils;
import com.aiatomic.couting.util.HttpUtils;
import com.aiatomic.couting.util.SystemUtil;
import com.aiatomic.couting.widgets.GoodAdapter;
import com.aiatomic.couting.widgets.LoadingDialog;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class FragmentAccountInfoShop extends Fragment {
	private View view;
	private AccountInfoActivity activity;
	private GridView gridView_goods;
	private GoodAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_account_info_shop, container, false);
		activity = (AccountInfoActivity) getActivity();
		gridView_goods = (GridView) view.findViewById(R.id.gridViewGoods);
		List<GoodEntity> objects = new ArrayList<GoodEntity>();
		adapter = new GoodAdapter(activity, R.layout.custom_good_list_item, objects);
		adapter.clear();
		gridView_goods.setAdapter(adapter);
		bindEvent();
		return view;
	}

	private void bindEvent() {
		gridView_goods.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (!AppUtils.isLogin(getActivity())) {
					Intent intent = new Intent(getActivity(), LoginActivity.class);
					getActivity().startActivity(intent);
				}else{
					GoodEntity good=adapter.getItem(position);
					activity.addOrder(good.getSb_good_id());
				}
			}
		});
		for (GoodEntity entity:AppUtils.goods) {
			adapter.add(entity);
		}
		adapter.notifyDataSetChanged();
	}
	
}
